(function ($, window, document, undefined) {
    $.ajaxSettings.beforeSend = function (xhr, setting) {
        jQuery.myloading();
    };
    // 设置全局complete
    $.ajaxSettings.complete = function (xhr, status) {
        jQuery.myloading("hide");
    }
    var w = window;
    var self;
    var FATTEND_DETAIL_AJAX_URL = "../../public/android/flow/show?";// 流程详情
    var _FATTEND_DETAIL_AJAX_URL = "public/android/flow/show?";// 流程详情，用于 5+ App

    var FLOW_INIT_AJAX_URL = "../../public/android/flow/init?";// 发起流程
    var _FLOW_INIT_AJAX_URL = "public/android/flow/init?";// 发起流程，用于 5+ App

    var FLOW_DISPOSE_AJAX_URL = "../../public/android/flow/dispose?"// 流程处理
    var _FLOW_DISPOSE_AJAX_URL = "public/android/flow/dispose?"// 流程处理

    var FLOW_VIEW_JS_URL = "../../public/android/flow/getViewJs?"// 显示规则
    var _FLOW_VIEW_JS_URL = "public/android/flow/getViewJs?"// 显示规则

    // 20210706改为finishAction
    // var FREE_FLOW_DISPOSE_AJAX_URL= "../../public/flow_dispose_free_do.jsp";// 自由流程处理
    // var _FREE_FLOW_DISPOSE_AJAX_URL= "public/flow_dispose_free_do.jsp";// IOS自由流程处理

    var FREE_FLOW_DISPOSE_AJAX_URL = "../../public/android/flow/finishActionFree";// 自由流程处理
    var _FREE_FLOW_DISPOSE_AJAX_URL = "public/android/flow/finishActionFree";// IOS自由流程处理

    // var PRESET_FLOW_DISPOSE_AJAX_URL= "../../public/flow_dispose_do.jsp";// 预定流程处理
    // var _PRESET_FLOW_DISPOSE_AJAX_URL= "public/flow_dispose_do.jsp";// IOS预定流程处理

    var PRESET_FLOW_DISPOSE_AJAX_URL = "../../public/android/flow/finishAction";// 预定流程处理
    var _PRESET_FLOW_DISPOSE_AJAX_URL = "public/android/flow/finishAction";// IOS预定流程处理

    var FLOW_RETURN_AJAX_URL = "../../public/android/flow/getreturn";
    var _FLOW_RETURN_AJAX_URL = "public/android/flow/getreturn";

    var FLOW_MULTI_DEPT = "../../public/android/flow/multiDept";
    var _FLOW_MULTI_DEPT = "public/android/flow/multiDept";

    var FLOW_TRANSFER_AJAX_URL = "../../flow/transfer";
    var _FLOW_TRANSFER_AJAX_URL = "flow/transfer";

    // 当发起人选择节点上的用户后或表单中指定的人员变动后，重新匹配分支及人员
    var FLOW_MOBILE_MATCHBRANCHANDUSER_AJAX_URL = "../../public/android/flow/matchBranchAndUser";
    var _FLOW_MOBILE_MATCHBRANCHANDUSER_AJAX_URL = "public/android/flow/matchBranchAndUser";

    // 用于发起人选择节点上人员时匹配分支
    var FLOW_MATCHBRANCHANDUSER_AJAX_URL = "../../flow/matchBranchAndUser";
    var _FLOW_MATCHBRANCHANDUSER_AJAX_URL = "flow/matchBranchAndUser";

    var FLOW_GETFIELDNAMESINCOND_AJAX_URL = "../../flow/getFieldNamesInCond";
    var _FLOW_GETFIELDNAMESINCOND_AJAX_URL = "flow/getFieldNamesInCond";

    var FLOW_GETACTIONSONBRANCH_AJAX_URL = "../../flow/getActionsOnBranch";
    var _FLOW_GETACTIONSONBRANCH_AJAX_URL = "flow/getActionsOnBranch";

    var FLOW_SETACTIONUSER_AJAX_URL = "../../flow/setActionUser";
    var _FLOW_SETACTIONUSER_AJAX_URL = "flow/setActionUser";

    var Form;

    var isStarted = false;  // 流程是否已开始
    var curActionId = -1;   // 当前处理的节点ID
    var deptLimited = '';  // 自选用户时，限定部门
    var deptFieldLimited = '';  // 自选用户时，限定部门表单域

    $.Flow = $.Class.extend({
        init: function (element, options) {
            this.element = element,
                this.default = {
                    "formSelector": ".mui-input-group",
                    "ulSelector": ".mui-table-view"
                }
            this.options = $.extend(true, this.default, options);
            Form = new $.Form(this.element, this.options);
        },
        flowAttendDetail: function () {
            var self = this;
            var skey = self.options.skey;
            var flowId = self.options.flowId;
            var ul = jQuery(self.options.ulSelector);
            var datas = {"skey": skey, "flowId": flowId}

            var url = FATTEND_DETAIL_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FATTEND_DETAIL_AJAX_URL;
            }
            mui.post(url, datas, function (data) {
                console.log('data', data);
                if (data.res != '0') {
                    mui.toast(data.msg);
                    return;
                }
                Form.initFlowDetail(data);

                if (!data.isRendWithoutViewRule) {
                    if (data.viewJs) {
                        var s0 = document.createElement('script');
                        s0.text = data.viewJs;
                        document.body.appendChild(s0);
                    }
                } else {
                    url = FLOW_VIEW_JS_URL;
                    if (mui.os.plus && mui.os.ios) {
                        // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                        var rootPath = this.getContextPath();
                        var p = rootPath.indexOf('/weixin');
                        if (p != -1) {
                            rootPath = rootPath.substring(0, p);
                        }
                        url = rootPath + "/" + _FLOW_VIEW_JS_URL;
                        console.log('url', url);
                    }
                    var params = {
                        flowId: flowId
                    }
                    $.post(url, params, function (data) {
                        if (data.viewJs) {
                            var s0 = document.createElement('script');
                            s0.text = data.viewJs;
                            document.body.appendChild(s0);
                        }
                    });
                }
            }, "json");
        },
        getContextPath: function () {
            var strFullPath = document.location.href;
            var strPath = document.location.pathname;
            var pos = strFullPath.indexOf(strPath);
            var prePath = strFullPath.substring(0, pos);
            var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
            return (prePath + postPath);
        },
        flowDisposeInit: async function () {  // 待办流程初始化接口 - 发起流程初始化接口
            var self = this;
            var content = self.element;
            var url = FLOW_DISPOSE_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_DISPOSE_AJAX_URL;
            }
            var skey = self.options.skey;
            var formCode = self.options.formCode;
            // console.log('skey=' + skey);
            var flagXorRadiate = false;
            var datas;
            if (myActionId == 0) {
                datas = {
                    "skey": skey,
                    "title": self.options.title,
                    "code": self.options.code,
                    "type": self.options.type
                }
                var extraData = self.options.extraData;
                // extraData = $.parseJSON(extraData);
                // 合并
                datas = $.extend({}, datas, extraData);
                url = FLOW_INIT_AJAX_URL;
                if (mui.os.plus && mui.os.ios) {
                    // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                    var rootPath = this.getContextPath();
                    var p = rootPath.indexOf('/weixin');
                    if (p != -1) {
                        rootPath = rootPath.substring(0, p);
                    }
                    url = rootPath + "/" + _FLOW_INIT_AJAX_URL;
                    console.log('url', url);
                }
            } else {
                datas = {"skey": skey, "myActionId": myActionId};
                var extraData = self.options.extraData;
                // extraData = $.parseJSON(extraData);
                datas = $.extend({}, datas, extraData);
            }

            // 使同步，同步后将无法显示loading效果，将本方法改为异步方法，用await $.post可实现同步并带有loading的效果
            // $.ajaxSettings.async = false;
            jQuery.myloading();
            await $.post(url, datas, function (data) {
                var res = data.res;
                var myAId = -1;
                if (res != '0') {
                    mui.toast(data.msg);
                    return;
                } else {
                    var actionId = data.actionId;
                    curActionId = actionId;

                    var flowId = data.flowId;
                    var annexs = data.result.annexs;
                    var fields = data.result.fields;
                    var hasAttach = data.hasAttach;
                    var isFlowStarted = data.isFlowStarted;
                    isStarted = data.isFlowStarted;

                    var isLight = data.isLight;
                    var btnContent = self.flowDisposeBtn(data);
                    flagXorRadiate = data.flagXorRadiate;

                    myAId = data.myActionId;
                    var cwsWorkflowTitle = data.cwsWorkflowTitle;
                    var commonParams = self.flowDisposeCommonParam(flowId, myAId, actionId, skey, cwsWorkflowTitle, data.flowIsRemarkShow, data.flowRemarkPos, data.rem, data.cwsWorkflowResult);

                    var isPlusBefore = data.isPlusBefore;
                    var isMyPlus = data.isMyPlus;
                    var plusDesc = data.plusDesc;

                    var isShowNextUsers = data.isShowNextUsers;
                    var isMobileForbidDownload = data.isMobileForbidDownload

                    if (isLight) {
                        // @输入框
                        var at_input_box = '';
                        at_input_box += '<div data-isnull="false" data-code="cwsWorkflowResult"><span style="display:none">内容</span><textarea type="text" name="cwsWorkflowResult" id="cwsWorkflowResult" class="at_textarea" placeholder="说点什么吧~"/></div>';
                        at_input_box += commonParams;
                        at_input_box += '<div class="at_user_div clearfix" isLight="true">';
                        at_input_box += '<a><span class="iconfont icon-at"></span><span>提醒谁看</span>';
                        at_input_box += '<div class="userDiv"></div>';
                        at_input_box += '</a>';
                        at_input_box += '</div>';
                        jQuery("#free_flow_form").append(at_input_box);
                        jQuery("#free_flow_form").addClass("submitFlow");
                        var annex = data.lightDetail; // 评论
                        jQuery(".mui-content").append(Form.initAtStep(annex));

                        jQuery(".mui-content").append(btnContent);
                    } else {
                        var title = data.cwsWorkflowTitle;// 标题
                        var c_t = '<div class="mui-input-row" style="display: none">';
                        // c_t += '<label style="color:#000;width:100%;font-weight:bold">'+title+'</label>';
                        c_t += '<input type="text" name="cwsWorkflowTitle" value="' + title + '"/>';
                        c_t += '</div>';
                        jQuery(".mui-input-group").append(c_t);
                        if (fields.length > 0) {
                            Form.initForms(actionId, flowId, fields, formCode, data, isMobileForbidDownload); // 初始化Form表单
                        }

                        // 赋予计算控件字段的值，allSums，因为有可能在手机端嵌套表中添加了记录，sum(...)计算成功了，但是主表并未保存或提交，那么计算控件中的值仍为0
                        if (data.allSums) {
                            var allSums = data.allSums;
                            console.log('allSums', allSums);
                            for (var k = 0; k < allSums.length; k++) {
                                var json = allSums[k];
                                calByNestSheet(json['sums'], json['formCode']);
                            }
                        }

                        if ("files" in data.result) {
                            var _files = data.result.files;
                            if (_files.length > 0) {
                                var _ul = Form.flowInitFiles(_files, false, isMobileForbidDownload);
                                jQuery(".mui-input-group").append(_ul);
                            }
                        }

                        // 照片区域
                        if (hasAttach) {
                            jQuery(".mui-input-group").append("<ul class='mui-input-row img-area'><li class='img-box' style='border:0px'><img class='capture_btn' src='../../static/images/camera.png'></li></ul>");
                        }

                        if ("users" in data.result) {
                            var users = data.result.users;
                            if (users.length > 0) {
                                var _flowNextUsers = self.flowNextUsers(flagXorRadiate, users);
                                jQuery(".mui-input-group").append(_flowNextUsers);
                                if (!isShowNextUsers) {
                                    jQuery('.user-area').hide();
                                }
                            }
                        }

                        if ("bindFieldList" in data.result) {
                            // 初始化表单中指定的人员和限定部门字段的侦听，不能用change事件，因为可能为表单域选择宏控件映射的字段
                            var bindFieldList = data.result.bindFieldList;
                            console.log('bindFieldList', bindFieldList);
                            var bindFieldListOldVal = new Array(bindFieldList.length);
                            for (let k in bindFieldList) {
                                let fieldName = bindFieldList[k];
                                bindFieldListOldVal[k] = '';
                                window.setInterval(function () {
                                    if (o(fieldName) && bindFieldListOldVal[k] != o(fieldName).value) {
                                        console.log(
                                            'bindFieldListOldVal[' + k + ']',
                                            bindFieldListOldVal[k],
                                            'o(fieldName).value',
                                            o(fieldName).value,
                                        );
                                        bindFieldListOldVal[k] = o(fieldName).value;

                                        var params = {
                                            op: 'reMatchUser',
                                            actionId: actionId,
                                            myActionId: data.myActionId,
                                            fieldName: fieldName,
                                            fieldValue: o(fieldName).value,
                                        };

                                        var url = FLOW_MOBILE_MATCHBRANCHANDUSER_AJAX_URL;
                                        if (mui.os.plus && mui.os.ios) {
                                            // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                                            var rootPath = this.getContextPath();
                                            var p = rootPath.indexOf('/weixin');
                                            if (p != -1) {
                                                rootPath = rootPath.substring(0, p);
                                            }
                                            url = rootPath + "/" + _FLOW_MOBILE_MATCHBRANCHANDUSER_AJAX_URL;
                                        }
                                        $.ajax({
                                            type: "post",
                                            url: url,
                                            data: params,
                                            dataType: "json",
                                            success: function (res, status) {
                                                console.log(res);
                                                if (res.res == 0) {
                                                    var users = res.users || [];
                                                    if (users.length > 0) {
                                                        jQuery('.user-box').remove();
                                                        var _flowNextUsers = self.flowNextUsers(flagXorRadiate, users);
                                                        jQuery(".mui-button-row").prepend(_flowNextUsers);
                                                        if (!isShowNextUsers) {
                                                            jQuery('.user-area').hide();
                                                        }
                                                    }
                                                } else {
                                                    mui.toast(res.msg);
                                                }
                                            },
                                            error: function (XMLHttpRequest, textStatus) {
                                                alert(XMLHttpRequest.responseText);
                                            }
                                        });
                                    }
                                }, 500);
                            }
                        }

                        if (data.isFree) {
                            var _flowNextUsers = self.flowNextUsersFree();
                            jQuery(".mui-input-group").append(_flowNextUsers);
                        }

                        if ("multiDepts" in data.result) {
                            var multiDepts = data.result.multiDepts;
                            if (multiDepts.length > 0) {
                                self.chooseMultiDepts(multiDepts);
                            }
                        }

                        // 加签描述区域
                        var dis = "";
                        if (!(isMyPlus && !isPlusBefore)) {
                            dis = "style='display:none'";
                        }
                        jQuery(".mui-input-group").append('<div id="plusDescBox" class="mui-input-row" ' + dis + '>' + plusDesc + '<span id="btnDelPlus">✕</span></div>');

                        // 回复区域
                        if ("isReply" in data) {
                            if (data.isReply) {
                                var annexGroup = '<ul class="mui-table-view reply-ul">';
                                var annexes = data.result.annexs;
                                $.each(annexes, function (index, item) {
                                    annexGroup += '<li class="mui-table-view-cell">';
                                    annexGroup += '	<div class="reply-header">';
                                    annexGroup += '		<span class="reply-name">' + item.annexUser + '</span>';
                                    // console.log("data.isProgress=" + data.isProgress);
                                    if (data.isProgress) {
                                        annexGroup += '	<span class="reply-progress">' + item.progress + '%</span>';
                                    }
                                    annexGroup += '		<span class="reply-date">' + item.add_date + '</span>';
                                    annexGroup += '	</div>';
                                    var content = highlightMentions(item.content);
                                    annexGroup += '	<div class="reply-content">' + content + '</div>';
                                    annexGroup += '</li>';
                                });
                                annexGroup += "</ul>";
                                jQuery('.reply-form').show();
                                jQuery('#progressLabel').text(data.progress);
                                jQuery('#progress').val(data.progress);
                                jQuery(".annex-group").append(annexGroup);
                            } else {
                                // 以免出现回复不能为空
                                jQuery('.reply-form').remove();
                            }
                        } else {
                            // 以免出现回复不能为空
                            jQuery('.reply-form').remove();
                        }

                        var formSelector = jQuery(".mui-input-group");
                        formSelector.append(commonParams);
                        formSelector.append('<div id="starterSelBox" class="mui-input-row"></div>');
                        formSelector.append(btnContent);
                        formSelector.addClass("submitFlow");

                        if (!data.isRendWithoutViewRule) {
                            if (data.viewJs) {
                                var s0 = document.createElement('script');
                                s0.text = data.viewJs;
                                document.body.appendChild(s0);
                            }
                        } else {
                            url = FLOW_VIEW_JS_URL;
                            if (mui.os.plus && mui.os.ios) {
                                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                                var rootPath = this.getContextPath();
                                var p = rootPath.indexOf('/weixin');
                                if (p != -1) {
                                    rootPath = rootPath.substring(0, p);
                                }
                                url = rootPath + "/" + _FLOW_VIEW_JS_URL;
                                console.log('url', url);
                            }
                            var params = {
                                actionId: actionId,
                                flowId: flowId
                            }
                            $.post(url, params, function (data) {
                                if (data.viewJs) {
                                    var s0 = document.createElement('script');
                                    s0.text = data.viewJs;
                                    document.body.appendChild(s0);
                                }
                            });
                        }

                        formSelector.append('<input id="deptOfUserWithMultiDept" name="deptOfUserWithMultiDept" type="hidden" />');
                    }

                    // 限定部门、限定部门表单域
                    deptLimited = data.result.deptLimited || '';
                    deptFieldLimited = data.result.deptFieldLimited || '';

                    // 如果是开始节点且存在审批人类型为发起者选人的节点，则匹配出条件分支，以便于发起人选择用户
                    if (!isFlowStarted && data.hasApproverSelectByStarter) {
                        self.getBranchMatchOnCondFieldChange(actionId, myAId, true, flagXorRadiate, isShowNextUsers);
                    }

                    // 如果是开始节点，则获取条件字段，以便于匹配条件，获得后续节点以选择人员
                    if (!isFlowStarted && flagXorRadiate) {
                        var url = FLOW_GETFIELDNAMESINCOND_AJAX_URL;
                        if (mui.os.plus && mui.os.ios) {
                            // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                            var rootPath = this.getContextPath();
                            var p = rootPath.indexOf('/weixin');
                            if (p != -1) {
                                rootPath = rootPath.substring(0, p);
                            }
                            url = rootPath + "/" + _FLOW_GETFIELDNAMESINCOND_AJAX_URL;
                        }

                        $.ajax({
                            type: "post",
                            url: url,
                            data: {
                                actionId: actionId
                            },
                            dataType: "json",
                            contentType: "application/x-www-form-urlencoded; charset=iso8859-1",
                            beforeSend: function (XMLHttpRequest) {
                            },
                            success: function (res, status) {
                                if (res.code == 200) {
                                    var condFields = res.data;
                                    console.log('condFields', condFields);
                                    condFields.forEach((item) => {
                                        console.log('tagName', fo(item).tagName);
                                        if (o(item).tagName == 'INPUT' && o(item).getAttribute('type') == 'radio') {
                                            jQuery('input[name=' + item + ']').click(function () {
                                                console.log('condField ' + item, jQuery(this).val());
                                                self.getBranchMatchOnCondFieldChange(actionId, myAId, false, flagXorRadiate, isShowNextUsers);
                                            });
                                        } else {
                                            jQuery(fo(item)).change(function () {
                                                console.log('condField ' + item, o(item).value);
                                                self.getBranchMatchOnCondFieldChange(actionId, myAId, false, flagXorRadiate, isShowNextUsers);
                                            });
                                        }
                                    });
                                } else {
                                    mui.toast(data.msg);
                                }
                            },
                            error: function (XMLHttpRequest, textStatus) {
                                alert(XMLHttpRequest.responseText);
                            }
                        });
                    }
                }
                mui(".mui-button-row").on("tap", ".back_submit", function () {
                    self.flowBackServer();
                });
                mui(".mui-button-row").on("tap", ".transfer_btn", function () {
                    selectUserWinForTransfer();
                });
                mui(".mui-button-row").on("tap", ".refuse_submit", function () {
                    var btnArray = ['否', '是'];
                    mui.confirm('您确定要拒绝么？', '提示', btnArray, function (e) {
                        if (e.index == 1) {
                            jQuery("#op").val("manualFinish");
                            self.flowSendServer();
                        }
                    });
                });
                // 提交按钮绑定事件
                mui(".mui-button-row").on("tap", ".flow_submit", function () {
                    var btnArray = ['是', '否'];
                    mui.confirm('您确定要提交么？', '提示', btnArray, function (e) {
                        if (e.index == 0) {
                            self.submitFlow();
                        }
                    });
                });
                // 保存草稿
                mui(".mui-button-row").on("tap", ".flow_draft", function () {
                    jQuery("#op").val("saveformvalue");
                    self.flowSendServer();
                });
                // 加签
                mui(".mui-button-row").on("tap", ".flow_plus", function () {
                    self.openPlusDlg(myAId, jQuery('.flow_plus').attr('isFlowStarted'));
                });
                mui("#plusDescBox").on("tap", "#btnDelPlus", function () {
                    var btnArray = ['是', '否'];
                    mui.confirm('您确定要取消加签么？', '提示', btnArray, function (e) {
                        if (e.index == 0) {
                            $.ajax({
                                type: "post",
                                url: "../../flow/delPlus",
                                data: {
                                    actionId: actionId,
                                },
                                dataType: "json",
                                success: function (res, status) {
                                    console.log(res);
                                    mui.toast(res.msg);
                                    if (res.code == 200) {
                                        jQuery('#plusDescBox').hide();
                                        jQuery('.flow_plus').show();
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus) {
                                    alert(XMLHttpRequest.responseText);
                                }
                            });
                        }
                    });
                });

                // 删除按钮绑定事件
                mui(".mui-button-row").on("tap", ".del_btn", function () {
                    var btnArray = ['否', '是'];
                    mui.confirm('您确定要删除么？', '提示', btnArray, function (e) {
                        if (e.index == 1) {
                            jQuery("#op").val("del");
                            self.flowSendServer();
                        }
                    });
                });
                mui(".mui-button-row").on("tap", ".finish_btn", function () {
                    var btnArray = ['否', '是'];
                    mui.confirm('您确定要结束么？', '提示', btnArray, function (e) {
                        if (e.index == 1) {
                            jQuery("#op").val("manualFinishAgree");
                            self.flowSendServer();
                        }
                    });
                });
                mui(".mui-button-row").on("tap", ".readed_btn", function () {
                    var btnArray = ['否', '是'];
                    mui.confirm('您确定要已阅么？', '提示', btnArray, function (e) {
                        if (e.index == 1) {
                            let formData = new FormData($('#flow_form')[0]);
                            $.ajax({
                                type: "post",
                                url: "../../flow/readed",
                                data: formData,
                                dataType: "json",
                                processData: false,
                                contentType: false,
                                success: function (res, status) {
                                    console.log(res);
                                    mui.alert(res.msg, '提示', function () {
                                        if (self.options.isUniWebview) {
                                            console.log('wx.miniProgram', wx.miniProgram);
                                            wx.miniProgram.getEnv(function (res) {
                                                console.log("当前环境：" + JSON.stringify(res));
                                                /*if (res.miniprogram) {
                                                }*/
                                            });
                                            wx.miniProgram.postMessage({
                                                data: {
                                                    res: 0,
                                                    url: ""
                                                },
                                            });
                                            uni.navigateBack();
                                        } else {
                                            mui.back();
                                        }
                                    });
                                },
                                error: function (XMLHttpRequest, textStatus) {
                                    alert(XMLHttpRequest.responseText);
                                }
                            });
                        }
                    });
                });

                self.bindDataEvent();
            }, "json");

            // $.ajaxSettings.async = true;
        },
        // 当分支条件中的字段值有变化的时候，重新匹配条件
        getBranchMatchOnCondFieldChange: function(actionId, myActionId, isOnLoad, flagXorRadiate, isShowNextUsers) {
            if (!isOnLoad) {
                o('op').value = 'saveformvalue';
                o('opType').value = "saveBeforeStarterMatchBranchAndUser";
                this.flowSendServer();
            }

            var url = FLOW_MATCHBRANCHANDUSER_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_MATCHBRANCHANDUSER_AJAX_URL;
            }

            $.ajax({
                type: "post",
                url: url,
                data: {
                    actionId: actionId,
                    myActionId: myActionId,
                    isMatchForStarterSelUser: true, // 为发起者选择用户而匹配条件分支
                },
                dataType: "json",
                contentType: "application/x-www-form-urlencoded; charset=iso8859-1",
                beforeSend: function (XMLHttpRequest) {
                },
                success: function (res, status) {
                    if (res.code == 200) {
                        console.log('getMatchBranchAndUser res', res);
                        if (res.data.errCode == -2) {
                            // 不需要显示info
                            return;
                        } else if (res.data.errCode == -1) {
                            // 如果条件不满足，在默认条件分支中匹配用户matchActionUser报错
                            console.log(res.data.info)
                            // $.toast(res.data.info);
                            return;
                        } else if (res.data.errCode == -3) {
                            // 条件****不匹配，请注意填写是否正确，重新填写后请点击提交按钮！
                            console.log(res.data.info)
                            // $.toast(res.data.info);
                            return;
                        }

                        var isMatchUserException = false;
                        if (res.data.hasOwnProperty('isMatchUserException')) {
                            isMatchUserException = res.data.isMatchUserException;
                        }

                        if (!isMatchUserException) {
                            if (res.data.toActions && res.data.toActions.length > 0) {
                                let actionId = res.data.toActions[0].id;
                                var url = FLOW_GETACTIONSONBRANCH_AJAX_URL;
                                if (mui.os.plus && mui.os.ios) {
                                    // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                                    var rootPath = this.getContextPath();
                                    var p = rootPath.indexOf('/weixin');
                                    if (p != -1) {
                                        rootPath = rootPath.substring(0, p);
                                    }
                                    url = rootPath + "/" + _FLOW_GETACTIONSONBRANCH_AJAX_URL;
                                }

                                // 取得所匹配分支线上的节点
                                $.ajax({
                                    type: "post",
                                    url: url,
                                    data: {
                                        actionId: actionId,
                                    },
                                    dataType: "json",
                                    success: function (res, status) {
                                        var data = res.data;
                                        console.log('GETACTIONSONBRANCH data', data);
                                        jQuery('#starterSelBox').html('');
                                        data.forEach((it)=> {
                                            var title = it.title;
                                            var id = it.id;
                                            var ActionApplicantSelMode = it.ActionApplicantSelMode;
                                            var ActionApplicantSelScope = it.ActionApplicantSelScope;
                                            var usersSelected = it.usersSelected;
                                            var ActionApplicantAssignedUsers = it.ActionApplicantAssignedUsers;
                                            var ActionApplicantAssignedRoles = it.ActionApplicantAssignedRoles;

                                            var titleHtml = '<div id="action_box_' + id + '" data-mode="' + ActionApplicantSelMode + '" data-roles="' + ActionApplicantAssignedRoles + '" class="starter-sel-action"><div class="starter-sel-action-title"><span>' + title + '</span>';
                                            // 指定用户范围，不显示添加按钮
                                            if (ActionApplicantSelScope != 1) {
                                                var roles = '';
                                                if (ActionApplicantAssignedRoles) {
                                                    ActionApplicantAssignedRoles.forEach((item)=> {
                                                        if (roles == '') roles = item.code;
                                                        else roles += ',' + item.code;
                                                    })
                                                }
                                                titleHtml += '<span class="mui-icon mui-icon mui-icon-plus btn-set-user" scope="' + ActionApplicantSelScope + '" selMode="' + ActionApplicantSelMode + '" roles="' + roles + '" actionId="' + id + '"></span>';
                                            }
                                            titleHtml += '</div></div>';
                                            jQuery('#starterSelBox').append(titleHtml);

                                            // 如果是指定的用户范围
                                            if (ActionApplicantSelScope == 1) {
                                                if (ActionApplicantAssignedUsers) {
                                                    ActionApplicantAssignedUsers.forEach((item)=> {
                                                        if (ActionApplicantSelMode == 0) {
                                                            jQuery('#action_box_' + id).append('<span class="mui-badge cust-badge"><input name="action_' + id + '" value="' + item.userName + '" data-realname="' + item.realName + '" type="radio" onclick="window.flow.checkActionUser(this, ' + id + ', ' + myActionId + ', ' + flagXorRadiate + ', ' + isShowNextUsers + ')"/><span class="badge-user" data-user="' + item.userName + '">' + item.realName + '</span></span>');
                                                        } else {
                                                            jQuery('#action_box_' + id).append('<span class="mui-badge cust-badge"><input name="action_' + id + '" value="' + item.userName + '" data-realname="' + item.realName + '" type="checkbox" onclick="window.flow.checkActionUser(this, ' + id + ',' + myActionId + ', ' + flagXorRadiate + ', ' + isShowNextUsers + ')"/><span class="badge-user" data-user="' + item.userName + '">' + item.realName + '</span></span>');
                                                        }
                                                    })

                                                    if (ActionApplicantSelMode == 0) {
                                                        // 如果已经选择了用户，则置radio的值
                                                        if (usersSelected && usersSelected.length > 0) {
                                                            setRadioValue('action_' + id, usersSelected[0].userName);
                                                        }
                                                    } else {
                                                        if (usersSelected && usersSelected.length > 0) {
                                                            var values = '';
                                                            usersSelected.forEach((item)=> {
                                                                if (values == '') values = item.userName; else values += ',' + item.userName;
                                                            })
                                                            setMultiCheckboxChecked('action_' + id, values);
                                                        }
                                                    }
                                                }
                                            } else {
                                                // 如果已经选择了用户
                                                if (usersSelected) {
                                                    usersSelected.forEach((item)=> {
                                                        jQuery('#action_box_' + id).append('<span class="mui-badge cust-badge"><span class="badge-user" data-user="' + item.userName + '" data-realname="' + item.realName + '">' + item.realName + '</span><span class="badge-closer" onclick="window.flow.closeBadge(this, ' + id + ', ' + item.userName + ', ' + myActionId + ', ' + flagXorRadiate + ', ' + isShowNextUsers + ')">×</span></span>');
                                                    })
                                                }
                                            }
                                        })

                                        jQuery('.btn-set-user').click(function() {
                                            var actionId = jQuery(this).attr('actionId');
                                            var roles = jQuery(this).attr('roles');
                                            var scope = jQuery(this).attr('scope');
                                            var chooseUsers = '';
                                            jQuery('#action_box_' + actionId).find('.badge-user').each(function() {
                                                if (chooseUsers == '') chooseUsers = jQuery(this).data('user');
                                                else chooseUsers += ',' + jQuery(this).data('user');
                                            })
                                            selectUserWinForStarterSelUser(skey, actionId, jQuery(this).attr('selMode') == 1, scope, roles, chooseUsers, myActionId, flagXorRadiate, isShowNextUsers);
                                        });
                                    },
                                    error: function (XMLHttpRequest, textStatus) {
                                        alert(XMLHttpRequest.responseText);
                                    }
                                });
                            }
                        }
                    } else {
                        mui.toast(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        // 当发起人从指定用户中单选或多选时
        checkActionUser: function(obj, id, myActionId, flagXorRadiate, isShowNextUsers) {
            var users = [];
            if (jQuery(obj).attr('type') == 'radio') {
                users.push({userName: jQuery(obj).val(), realName: jQuery(obj).data('realname')});
            } else {
                jQuery('#action_box_' + id).find('input[type=checkbox]').each(function() {
                    if (jQuery(this).prop('checked')) {
                        var json = {userName: jQuery(this).val(), realName: jQuery(this).data('realname')};
                        users.push(json);
                    }
                })
            }

            var url = FLOW_SETACTIONUSER_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_SETACTIONUSER_AJAX_URL;
            }
            var params = {
                id: id,
                users: users
            }
            $.ajax({
                type: "post",
                url: url,
                data: params,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (res, status) {
                    console.log('checkActionUser', res);
                    mui.toast(res.msg);
                    // 重新匹配节点上的人员
                    window.flow.mobileMatchBranchAndUser(id, myActionId, flagXorRadiate, isShowNextUsers);
                },
                error: function (XMLHttpRequest, textStatus) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        // 当发起人选择完用户后(按全部或角色选择)
        onSelUserForStarterSelUser: function(userNames, realNames, loginNames, skey, id, myActionId, flagXorRadiate, isShowNextUsers) {
            var self = this;
            var mode = jQuery('#action_box_' + id).data('mode');
            var roles = jQuery('#action_box_' + id).data('roles');
            console.log('onSelUserForStarterSelUser', userNames);

            jQuery('#action_box_' + id).find('.cust-badge').remove();
            var users = [];
            for (var i=0; i < userNames.length; i++) {
                var userName = userNames[i];
                var realName = realNames[i];
                users.push({userName: userName, realName: realName});
                jQuery('#action_box_' + id).append('<span class="mui-badge mui-badge-primary cust-badge"><span class="badge-user" data-user="' + userName + '" data-realname="' + realName + '">' + realName + '</span><span class="badge-closer" onclick="window.flow.closeBadge(this, ' + id + ', ' + userName + ', ' + myActionId + ', ' + flagXorRadiate + ',' + isShowNextUsers + ')">×</span></span>');
            }

            var url = FLOW_SETACTIONUSER_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_SETACTIONUSER_AJAX_URL;
            }
            var params = {
                id: id,
                users: users
            }
            $.ajax({
                type: "post",
                url: url,
                data: params,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (res, status) {
                    console.log(res);
                    if (res.data.res != 0) {
                        mui.toast(res.msg);
                    } else {
                        // 重新匹配节点上的人员
                        window.flow.mobileMatchBranchAndUser(id, myActionId, flagXorRadiate, isShowNextUsers);
                    }
                },
                error: function (XMLHttpRequest, textStatus) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        // 删除发起人所选人员
        closeBadge: function(obj, id, userName, myActionId, flagXorRadiate, isShowNextUsers) {
            jQuery(obj).parent().remove();

            var url = FLOW_SETACTIONUSER_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_SETACTIONUSER_AJAX_URL;
            }
            console.log('closeBadge url', url);
            var users = [];
            jQuery('#action_box_' + id).find('.badge-user').each(function() {
                var json = {userName: jQuery(this).data('user'), realName: jQuery(this).data('realname')};
                users.push(json);
            })
            var params = {
                id: id,
                users: users
            }
            $.ajax({
                type: "post",
                url: url,
                data: params,
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (res, status) {
                    console.log('closeBadge', res);
                    mui.toast(res.msg);
                    window.flow.mobileMatchBranchAndUser(id, myActionId, flagXorRadiate, isShowNextUsers);
                },
                error: function (XMLHttpRequest, textStatus) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        // 当发起人选择节点上的用户后，重新匹配分支及人员，显示匹配到的结果，注意参数 actionId 为分支节点的ID，不是当前节点的ID
        mobileMatchBranchAndUser: function(actionId, myActionId, flagXorRadiate, isShowNextUsers) {
            var self = this;
            var params = {
                actionId: curActionId,
                myActionId: myActionId,
            };

            var url = FLOW_MOBILE_MATCHBRANCHANDUSER_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_MOBILE_MATCHBRANCHANDUSER_AJAX_URL;
            }
            $.ajax({
                type: "post",
                url: url,
                data: params,
                dataType: "json",
                success: function (res, status) {
                    console.log(res);
                    if (res.res == 0) {
                        var users = res.users || [];
                        if (users.length > 0) {
                            jQuery('.user-box').remove();
                            var _flowNextUsers = self.flowNextUsers(flagXorRadiate, users);
                            jQuery("#plusDescBox").before(_flowNextUsers);
                            if (!isShowNextUsers) {
                                jQuery('.user-area').hide();
                            }
                        }
                    } else {
                        mui.toast(res.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        submitFlow: function(customBtnParams) {
            var self = this;
            // 如果flagXorRadiate为false，有可能是条件分支，但却不带条件，所以此处仍需将所选人员对应的分支线传至服务器端，以便于手工选择
            jQuery('.cls-XorNextActionInternalNames').remove(); // 清空XorNextActionInternalNames，因为之前的提交可能会不成功，但会生成此隐藏域
            var _ckChecked = jQuery(".next_user_ck:checked");
            if (_ckChecked.length > 0) {
                _ckChecked.each(function (i) {
                    var _curCk = jQuery(this);
                    var _internalname = _curCk.data("internalname");
                    // 当存在分支，且分支为自选用户时，分支前的checkbox是没有internalname的
                    if (_internalname != null) {
                        // 如果已存在，则不生成
                        if (!jQuery('name[XorNextActionInternalNames=' + _internalname + ']')[0]) {
                            jQuery("#flow_form").append('<input type="hidden" class="cls-XorNextActionInternalNames" name="XorNextActionInternalNames" value="' + _internalname + '" />');
                        }
                    }
                })
            }

            jQuery("#op").val("finish");
            self.flowSendServer(customBtnParams);
        },
        // 打开加签窗对话框
        openPlusDlg: function (myActionId, isFlowStarted) {
            console.log('openPlusDlg isFlowStarted', isFlowStarted);
            var skey = jQuery("#skey").val();
            var strHtml = '<ul class="mui-table-view">';
            strHtml += '<li class="mui-table-view-cell mui-left">';
            strHtml += '加签类型';
            strHtml += '</li>';
            // 注意isFlowStarted为字符串型
            if (isFlowStarted == 'true') {
                strHtml += '<li class="mui-table-view-cell mui-left mui-radio">';
                strHtml += '<input type="radio" name="plusType" value="0" onclick="window.flow.onClickPlusType()" />前加签';
                strHtml += '</li>';
            }
            strHtml += '<li class="mui-table-view-cell mui-left mui-radio">';
            strHtml += '<input type="radio" name="plusType" value="1" onclick="window.flow.onClickPlusType()" />后加签';
            strHtml += '</li>';
            strHtml += '<li class="mui-table-view-cell mui-left mui-radio">';
            strHtml += '<input type="radio" name="plusType" value="2" onclick="window.flow.onClickPlusType()" />并签';
            strHtml += '</li>';
            strHtml += '<li class="mui-table-view-cell mui-left plus-mode">';
            strHtml += '审批方式';
            strHtml += '</li>';
            strHtml += '<li class="mui-table-view-cell mui-left mui-radio plus-mode">';
            strHtml += '<input type="radio" name="plusMode" value="0" />顺序审批';
            strHtml += '</li>';
            strHtml += '<li class="mui-table-view-cell mui-left mui-radio plus-mode">';
            strHtml += '<input type="radio" name="plusMode" value="1" />只需其中一人处理';
            strHtml += '</li>';
            strHtml += '<li class="mui-table-view-cell mui-left mui-radio plus-mode">';
            strHtml += '<input type="radio" name="plusMode" value="2" />全部审批';
            strHtml += '</li>';
            strHtml += '</ul>';
            strHtml += '<div class="mui-button-row" style="margin:10px;">';
            strHtml += '<button class="mui-btn mui-btn-primary" type="button" onclick="selectUserWinForPlus(getRadioValue(\'plusType\'), getRadioValue(\'plusMode\'), ' + myActionId + ',\'' + skey + '\');">选择人员</button>';
            strHtml += '</div>';
            var pop = new Popup({contentType: 2, isReloadOnClose: false, width: 340, height: 300});
            pop.setContent("contentHtml", strHtml);
            pop.setContent("title", "加签");
            pop.build();
            pop.show();
            jQuery('#dialogBox').css('background-color', '#fff');
        },
        onClickPlusType: function() {
            var plusType = getRadioValue('plusType');
            if (plusType == 2) {
                jQuery('.plus-mode').hide();
            } else {
                jQuery('.plus-mode').show();
            }
        },
        onSelUserForPlus: function (userNames, realNames, plusType, plusMode, myActionId) {
            var self = this;
            $.ajax({
                type: "post",
                url: "../../flow/plus",
                data: {
                    users: userNames,
                    myActionId: myActionId,
                    type: plusType,
                    mode: plusMode,
                },
                dataType: "json",
                success: function (res, status) {
                    var data = res.data;
                    plusDesc = data.plusDesc;
                    if (data.ret == 0) {
                        mui.toast(data.msg);
                    } else {
                        mui.toast(data.msg);
                        // 如果是前加签，则直接返回
                        if (plusType == 0) {
                            if (self.options.isUniWebview) {
                                wx.miniProgram.getEnv(function (res) {
                                    console.log("当前环境：" + JSON.stringify(res));
                                    /*if (res.miniprogram) {
                                    }*/
                                });
                                wx.miniProgram.postMessage({
                                    data: {
                                        res: 0,
                                        url: ""
                                    },
                                });
                                uni.navigateBack();
                            } else {
                                mui.back();
                            }
                        } else {
                            jQuery('#plusDescBox').html(plusDesc + '<span id="btnDelPlus">✕</span>').show();
                            jQuery('.flow_plus').hide();
                        }
                    }
                },
                error: function (XMLHttpRequest, textStatus) {
                    alert(XMLHttpRequest.responseText);
                }
            });
        },
        onSelUserForTransfer: function(userNames) {
            var self = this;
            var _data = {
                "skey": jQuery("#skey").val(),
                "myActionId": jQuery("#myActionId").val(),
                "toUserName": userNames
            };
            var url= FLOW_TRANSFER_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_TRANSFER_AJAX_URL;
            }
            jQuery.ajax(url, {
                dataType: 'json',// 服务器返回json格式数据
                type: 'post',// HTTP请求类型
                data: _data,
                beforeSend: function (XMLHttpRequest) {
                    jQuery.myloading();
                },
                complete: function (XMLHttpRequest, status) {
                    jQuery.myloading("hide");
                },
                success: function (res) {
                    var msg = res.msg;
                    $.toast(msg);
                    var code = res.code;
                    if (code == 200) {
                        if (self.options.isUniWebview) {
                            wx.miniProgram.getEnv(function (res) {
                                console.log("当前环境：" + JSON.stringify(res));
                            });
                            wx.miniProgram.postMessage({
                                data: {
                                    res: 0,
                                    url: ""
                                },
                            });
                            uni.navigateBack();
                        } else {
                            mui.back();
                        }
                    }
                },
                error: function (xhr, type, errorThrown) {
                    // 异常处理；
                    console.log(type);
                }
            });
        },
        onSelUserForAtUserInReply: function(userNames, realNames, loginNames, skey) {
            var self = this;
            var _data = {
                "skey": jQuery("#skey").val(),
                "atUsers": userNames
            };


            let atUsers = '';
            loginNames.forEach((item) => {
                if (atUsers == '') atUsers = ' @' + item + '  ';
                else atUsers += '@' + item + '  ';
            });

            // 获取文本区域元素
            const textarea = o('replyContent');
            if (!textarea) return;

            console.log('textarea', textarea);

            // 获取光标位置
            const startPos = textarea.selectionStart;
            const endPos = textarea.selectionEnd;

            // 保存当前光标之前的文本
            const beforeText = textarea.value.substring(0, startPos);
            // 保存当前光标之后的文本
            const afterText = textarea.value.substring(endPos, textarea.value.length);

            // 在光标位置插入文本
            textarea.value = beforeText + atUsers + afterText;

            // 更新光标位置到插入文本的末尾
            textarea.selectionStart = startPos + atUsers.length;
            textarea.selectionEnd = startPos + atUsers.length;
        },
        flowBackServer: function () {
            var self = this;

            // 退回时不能根据data-isnull校验必填，因为可能有的必填项是提交时必填，而非退回时必填
            // 退回时验证数据合法性
            if (typeof checkOnReturnBack == 'function') {
                try {
                    // 在form_js_formCode.js中写此方法
                    var r = checkOnReturnBack();
                    if (r != '') {
                        $.toast(r);
                        return;
                    }
                } catch (e) {
                    console.error(e);
                }
            }

            var _data = {
                "skey": jQuery("#skey").val(),
                "myActionId": jQuery("#myActionId").val(),
                "flowId": jQuery("#flowId").val()
            };
            var url = FLOW_RETURN_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_RETURN_AJAX_URL;
            }
            jQuery.ajax(url, {
                dataType: 'json',// 服务器返回json格式数据
                type: 'post',// HTTP请求类型
                data: _data,
                beforeSend: function (XMLHttpRequest) {
                    jQuery.myloading();
                },
                complete: function (XMLHttpRequest, status) {
                    jQuery.myloading("hide");
                },
                success: function (data) {
                    var res = data.res;
                    if (res == "0") {
                        if ("users" in data) {
                            var users = data.users;
                            if (users.length > 0) {
                                self.backUsers(users);
                            }
                        }
                    } else {
                        var msg = data.msg;
                        $.toast(msg);
                    }
                },
                error: function (xhr, type, errorThrown) {
                    // 异常处理；
                    console.log(type);
                }
            });
        },
        flowSendServer: function (customBtnParams) {
            var self = this;
            // console.log("jQuery('#op').val()=" + jQuery('#op').val());
            // 防止保存草稿、退回、拒绝时报“回复 不能为空”
            if (jQuery('#op').val() != "saveformvalue" && jQuery('#op').val() != "return" && jQuery('#op').val() != "manualFinish") {
                var _tips = "";

                jQuery("div[data-fieldtype='2'],div[data-fieldtype='3'],div[data-fieldtype='5'],div[data-fieldtype='6'],div[data-fieldtype='9']").each(function (i) {
                    var _code = jQuery(this).data("code");
                    var _val = jQuery("#" + _code).val();

                    if (_val != '' && !isNumeric(_val)) {
                        var _text = jQuery(this).find("span:first").text();
                        if (mui.os.plus) {
                            _tips += _text + " 须为数字！\n"
                        } else {
                            _tips += _text + " 须为数字！<br/>"
                        }
                    }
                });

                var cwsHiddenFields = '';
                jQuery("div[data-isnull='false']").each(function (i) {
                    console.log('data-isnull', this);
                    // 隐藏元素不作检查
                    if (window.getComputedStyle(this).display === "none") {
                        if (cwsHiddenFields == '')
                            cwsHiddenFields = jQuery(this).data("code");
                        else
                            cwsHiddenFields += ',' + jQuery(this).data("code");
                        return;
                    }

                    // 如果是嵌套表格，则不检查是否必填，由后台检查
                    if (jQuery(this).find('.nestSheetSelect')[0]) {
                        return;
                    }
                    // 如果是图像宏控件，则不检查是否必填，由后台检查
                    if (jQuery(this).find('.capture_btn')[0]) {
                        return;
                    }
                    // 如果是文件宏控件，则不检查是否必填，由后台检查
                    if (jQuery(this).find('input[type="file"]')[0]) {
                        return;
                    }

                    var _code = jQuery(this).data("code");
                    var _val = jQuery("#" + _code).val();

                    // 防止当提交时报“回复 不能为空”
                    if (jQuery('#op').val() == "finish" || jQuery('#op').val() == "del") {
                        if (_code == "content") {
                            // 通过判断其父节点的class是否为reply-form，确定是否为回复
                            if (jQuery(this).parent().attr("class") == "reply-form") {
                                return;
                            }
                        }
                    }

                    if (_val == undefined || _val == "") {
                        var _text = jQuery(this).find("span:first").text();
                        // console.log("_code=" + _code + " " + _text + " 不能为空！");
                        if (mui.os.plus) {
                            _tips += _text + " 不能为空！\n";
                        } else {
                            _tips += _text + " 不能为空！<br/>";
                        }
                    }
                });

                jQuery('#cwsHiddenFields').val(cwsHiddenFields);

                // 检查发起节点上是否还未选择相关节点上的用户
                if (!isStarted) {
                    var isSelected = true;
                    jQuery('.starter-sel-action').each(function() {
                        console.log('.starter-sel-action');
                        // 查找是否有input (按指定人员范围选择）
                        var hasInput = false;
                        var isThisActionUserSelected = false;
                        jQuery(this).find('input[name^=action_]').each(function() {
                            hasInput = true;
                            if (jQuery(this).prop('checked')) {
                                isThisActionUserSelected = true;
                                return false;
                            }
                        })
                        // 如果不含input，则说明是在全部或指定角色范围内选择
                        var hasBadgeUser = false;
                        if (!hasInput && !isThisActionUserSelected) {
                            if (jQuery(this).find('.badge-user').length == 0) {
                                hasBadgeUser = false;
                                isThisActionUserSelected = false;
                            } else {
                                hasBadgeUser = true;
                                isThisActionUserSelected = true;
                            }
                        }

                        // 既没有单选框、复选框，也没有选择用户
                        if (!hasInput && !hasBadgeUser) {
                            isSelected = false;
                            return false;
                        } else {
                            if (!isThisActionUserSelected) {
                                isSelected = false;
                                return false;
                            }
                        }
                    })
                    if (!isSelected) {
                        _tips += '请选择节点上的人员';
                    }
                }

                if (_tips != null && _tips != "") {
                    $.toast(_tips);
                    return;
                }
            }

            var isLight = jQuery(".flow_submit").attr("isLight");
            var isFree = jQuery(".flow_submit").attr("isFree");
            if (jQuery('#op').val() != "saveformvalue" && (isFree == 'true' || isLight == 'true')) {
                if (!jQuery("input[name='nextUsers']")[0]) {
                    var btnArray = ['否', '是'];
                    mui.confirm('您还没有选择下一步的用户，确定办理完毕了么？', '提示', btnArray, function (e) {
                        if (e.index == 1) {
                            self.flowSendServerPost();
                        }
                    });
                } else {
                    self.flowSendServerPost();
                }
            } else {
                self.flowSendServerPost(customBtnParams);
            }
        },
        flowSendServerPost: function (customBtnParams) {
            console.log('customBtnParams', customBtnParams);
            var self = this;
            var isLight = jQuery(".flow_submit").attr("isLight");
            var isFree = jQuery(".flow_submit").attr("isFree");
            var formData;
            var ajax_url = PRESET_FLOW_DISPOSE_AJAX_URL;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _PRESET_FLOW_DISPOSE_AJAX_URL;
            }

            if (isLight == 'true') {
                ajax_url = FREE_FLOW_DISPOSE_AJAX_URL;
                if (mui.os.plus && mui.os.ios) {
                    // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                    var rootPath = this.getContextPath();
                    var p = rootPath.indexOf('/weixin');
                    if (p != -1) {
                        rootPath = rootPath.substring(0, p);
                    }
                    url = rootPath + "/" + _FREE_FLOW_DISPOSE_AJAX_URL;
                }
                formData = new FormData($('#free_flow_form')[0]);
            } else if (isFree == 'true') {
                ajax_url = FREE_FLOW_DISPOSE_AJAX_URL;
                if (mui.os.plus && mui.os.ios) {
                    // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                    var rootPath = this.getContextPath();
                    var p = rootPath.indexOf('/weixin');
                    if (p != -1) {
                        rootPath = rootPath.substring(0, p);
                    }
                    url = rootPath + "/" + _FREE_FLOW_DISPOSE_AJAX_URL;
                }
                formData = new FormData($('#flow_form')[0]);
            } else {
                formData = new FormData($('#flow_form')[0]);
            }
            for (i = 0; i < blob_arr.length; i++) {
                var _blobObj = blob_arr[i];
                var field = "upload";
                if (_blobObj.field) {
                    field = _blobObj.field; // 图像宏控件的
                }
                formData.append(field, _blobObj.blob, _blobObj.fname);
            }

            // 可视化宏控件处理
            for (i = 0; i < uploaderCtlArr.length; i++) {
                var arr = uploaderCtlArr[i].getBlobArr();
                if (arr && arr[0]) {
                    formData.delete(arr[0].field);
                }
                for (var j = 0; j < arr.length; j++) {
                    var _blobObj = arr[j];
                    console.log('_blobObj', _blobObj);
                    var field = _blobObj.field; // 图像宏控件的
                    formData.append(field, _blobObj.blob, _blobObj.fname);
                }
            }

            // console.info(formData);
            jQuery.ajax(ajax_url, {
                dataType: 'json',// 服务器返回json格式数据
                async: false,
                type: 'post',// HTTP请求类型
                data: formData,
                processData: false,
                contentType: false,
                beforeSend: function (XMLHttpRequest) {
                    jQuery.myloading();
                },
                complete: function (XMLHttpRequest, status) {
                    jQuery.myloading("hide");
                },
                success: function (data) {
                    console.log('data', data);
                    var res = data.res;

                    if (o('opType').value == 'saveBeforeStarterMatchBranchAndUser') {
                        o('opType').value = '';
                        return;
                    }

                    if (res == "0") {
                        var nextMyActionId = data.nextMyActionId;
                        var open_url = '';
                        var title = '操作成功!'
                        var isBack = true;
                        // 20220523手机端不宜继续处理下一步，因为处理完以后，会回到之前的处理页面，看起来就象没退出
                        if (false && "nextMyActionId" in data && nextMyActionId != '') {
                            isBack = false;
                            title = '操作成功！请点击确定，继续处理下一步！';
                            open_url = "../flow/flowDispose?skey=" + skey + "&myActionId=" + nextMyActionId + "&isUniWebview=" + self.options.isUniWebview;
                        } else {
                            // 如果打开待办流程页面，点击后退按钮，会回退至流程处理dispose页面，故采用返回的方式
                            // open_url = "../flow/flow_doing_or_return.jsp?skey="+skey;
                        }

                        console.log('customBtnParams', customBtnParams);
                        mui.alert(title, '提示', function () {
                            if (isBack) {
                                // 下行在钉钉中会报错，具体错误不详，需安装钉钉开发版才能调试
                                // if(isDingTalk()) {
                                // 钉钉中流程处理结束后跳转至待办列表页面
                                if (/dingtalk/i.test(window.navigator.userAgent)) {
                                    uni.navigateTo({
                                        url: `/application/waitProcessListH5`
                                        // url: `/pages/applications/applications`
                                    });
                                    return;
                                }

                                console.log('isBack', isBack);
                                if (self.options.isUniWebview) {
                                    console.log('wx.miniProgram', wx.miniProgram);
                                    wx.miniProgram.getEnv(function (res) {
                                        console.log("当前环境：" + JSON.stringify(res));
                                        /*if (res.miniprogram) {
                                        }*/
                                    });
                                    wx.miniProgram.postMessage({
                                        data: {
                                            res: 0,
                                            url: ""
                                        },
                                    });
                                    uni.navigateBack();
                                } else {
                                    mui.back();
                                }
                            } else {
                                mui.openWindow({
                                    "url": open_url
                                })
                            }

                            // 表单中的按钮
                            if (customBtnParams) {
                                var btnParams = encodeURI(JSON.stringify(customBtnParams));
                                if (mui.os.plus) {
                                    var record = {
                                        op: 'opBtnFlow',
                                        flowType: customBtnParams.flowType,
                                        flowTypeCode: customBtnParams.flowTypeCode,
                                        btnId: customBtnParams.btnId,
                                        flowId: customBtnParams.flowId,
                                        moduleId: customBtnParams.moduleId,
                                        btnFieldName: customBtnParams.btnFieldName,
                                        moduleCode: customBtnParams.moduleCode
                                    };
                                    record = JSON.stringify(record);
                                    console.log('record', record);
                                    uni.navigateTo({
                                        url: `/application/processInit?record=${record}`
                                    });
                                } else {
                                    // 按下面的方式打开后，没有标题及后退按钮
                                    mui.openWindow({
                                        url: `../flow/flowDispose?op=opBtnFlow&flowTypeCode=${customBtnParams.flowTypeCode}&btnParams=${btnParams}&skey=${skey}&isUniWebview=${isUniWebview}`
                                    })
                                }
                            }
                        });
                    } else if (res == "3") {
                        var users = data.users;
                        if (users.length == 0) {
                            $.toast("没有满足条件的分支或人员");
                        } else {
                            self.conditionBranch(users);
                        }
                    } else {
                        var msg = data.msg;
                        if (!mui.os.plus) {
                            msg = msg.replace(/\r\n|\n|\r/g, "<br />");
                        }
                        $.toast(msg);

                        // 保存草稿后刷新，如果myAction为0表示发起流程，此时刷新就成了新发起一条流程
                        if (data.op == 'saveformvalue' && myActionId != 0)
                            window.location.reload();
                    }
                },
                error: function (xhr, type, errorThrown) {
                    $.toast(type);
                    console.log(type);
                }
            });
        },
        // 流程提交通用参数
        flowDisposeCommonParam: function (flowId, myActionId, actionId, skey, cwsWorkflowTitle, flowIsRemarkShow, flowRemarkPos, rem, cwsWorkflowResult) {
            var params = '';
            if (flowIsRemarkShow/* && flowRemarkPos == 1*/) {
                params += '<div class="mui-input-row">';
                params += '<label>' + rem + '</label>';
                params += '<div class="cwsWorkflowResult-box"><textarea id="cwsWorkflowResult" name="cwsWorkflowResult">' + cwsWorkflowResult + '</textarea></div>';
                params += '</div>';
            } else {
                params += '	<input type="hidden" name="cwsWorkflowResult" id="cwsWorkflowResult" />';
            }
            params += '<input type="hidden" name="expireHours" value="0"/>';
            params += '	<input type="hidden" name="isToMobile" value="true"/>';
            params += '	<input type="hidden" id="flowId" name="flowId" value="' + flowId + '"/>';
            params += '	<input type="hidden" id="myActionId" name="myActionId" value="' + myActionId + '"/>';
            params += '	<input type="hidden" name="isUseMsg" value="true"/>';
            params += '	<input type="hidden" name="cws_lontitude" value=""/>';
            params += '	<input type="hidden" name="cws_latitude" value=""/>';
            params += '	<input type="hidden" name="cws_address" value=""/>';
            params += '	<input type="hidden" name="actionId" value="' + actionId + '"/>';
            params += '	<input type="hidden" id="skey" name="skey" value="' + skey + '"/>';
            params += '	<input type="hidden" name="op" id="op" value="finish"/>';
            params += '	<input type="hidden" name="opType" value=""/>';
            return params;
        },
        flowDisposeBtn: function (data) {
            var isLight = data.isLight;
            var canDecline = data.canDecline;
            var canTransfer = data.canTransfer;
            var canReturn = data.canReturn;
            var hasAttach = data.hasAttach;
            var canDel = data.canDel;
            var canFinishAgree = data.canFinishAgree;
            var canReaded = data.canReaded;
            var btnContent = '<div class="mui-button-row">';
            var isFree = data.isFree;
            var isBtnSaveShow = data.isBtnSaveShow;
            var btnAgreeName = data.btnAgreeName ? data.btnAgreeName : '提交';
            var isActionKindRead = data.isActionKindRead;
            if (isActionKindRead) {
                btnAgreeName = data.btnReadName;
            }
            var btnRefuseName = data.btnRefuseName;
            var canPlus = data.canPlus;
            var isFlowStarted = data.isFlowStarted;

            if (!btnRefuseName) {
                btnRefuseName = '拒绝';
            }
            var btnReturnName = data.btnReturnName;
            if (!btnReturnName) {
                btnReturnName = '退回';
            }
            var btnTransferName = data.btnTransferName;
            if (!btnTransferName) {
                btnTransferName = '转办';
            }
            var isFlowReturnWithRemark = data.isFlowReturnWithRemark;
            if (!isFlowReturnWithRemark) {
                isFlowReturnWithRemark = false;
            }
            var btnReadedName = data.btnReadedName;
            if (!btnReadedName) {
                btnReadedName = '已阅';
            }
            var btnSaveName = data.btnSaveName;
            if (!btnSaveName) {
                btnSaveName = '保存';
            }
            var btnFinishAgreeName = data.btnFinishAgreeName;
            if (!btnFinishAgreeName) {
                btnFinishAgreeName = '结束';
            }

            if (!isLight && isBtnSaveShow) {
                btnContent += '<button type="button" class="mui-btn mui-btn-primary mui-btn-outlined flow_draft">' + btnSaveName + '</button>';
            }
            if (canPlus) {
                btnContent += ' <button type="button" style="margin-left:5px;" isFlowStarted="' + isFlowStarted + '" class="mui-btn mui-btn-primary mui-btn-outlined flow_plus">加签</button>';
            }
            btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined flow_submit" isFree="' + isFree + '" isLight=' + isLight + '>' + btnAgreeName + '</button>';
            if (canDel) {
                btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined del_btn">删除</button>';
            }
            /*if (hasAttach) {
                btnContent += '	<button style="margin-left:5px;" type="button" captureFieldName="upload" class="mui-btn mui-btn-primary mui-btn-outlined capture_btn">照片</button>';
            }*/
            if (!isLight && canDecline == 'true') {
                btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined refuse_submit">' + btnRefuseName + '</button>';
            }
            if (!isLight && canReturn == 'true') {
                btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined back_submit" isFlowReturnWithRemark="' + isFlowReturnWithRemark + '">' + btnReturnName + '</button>';
            }
            if (!isLight && canTransfer && isFlowStarted) {
                btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined transfer_btn">' + btnTransferName + '</button>';
            }
            if (canFinishAgree) {
                btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined finish_btn">' + btnFinishAgreeName + '</button>';
            }
            if (canReaded) {
                btnContent += '	<button style="margin-left:5px;" type="button" class="mui-btn mui-btn-primary mui-btn-outlined readed_btn">' + btnReadedName + '</button>';
            }
            btnContent += '</div>';
            return btnContent;
        },
        chooseMultiDepts: function (multiDepts) {
            var self = this;
            var deptContent = '<div class="mui-input-row dept_title"><label>请选择你所在部门:</label></div>';
            deptContent += '<div class="mui-row multi_dept_div">';
            $.each(multiDepts, function (index, item) {
                var _name = item.name;
                var _code = item.code;
                deptContent += '<span class="mui-checkbox mui-left user_ck_span " style="float: left;">';
                deptContent += '<label style="line-height: 45px;">';
                deptContent += _name;
                deptContent += '</lable>';
                deptContent += '<input name="multi_dept_ck" value="' + _code + ' "  type="checkbox"  class="multi_dept_ck" />'
                deptContent += '</span>';
            });
            deptContent += '</div>';
            jQuery(".mui-input-group").append(deptContent);
            mui(".mui-input-group").on("tap", ".multi_dept_ck", function () {
                var _value = jQuery(this).val();
                self.getUsersByDepts(_value);
            });
        },
        getUsersByDepts: function (deptCode) {
            var self = this;
            var _data = {
                "skey": jQuery("#skey").val(),
                "myActionId": jQuery("#myActionId").val(),
                "deptCode": deptCode
            };
            var url = FLOW_MULTI_DEPT;
            if (mui.os.plus && mui.os.ios) {
                // 将FLOW_INIT_AJAX_URL路径改为完整的路径，否则ios中5+app会因为spring security不允许url中包括../而致无法访问
                var rootPath = this.getContextPath();
                var p = rootPath.indexOf('/weixin');
                if (p != -1) {
                    rootPath = rootPath.substring(0, p);
                }
                url = rootPath + "/" + _FLOW_MULTI_DEPT;
            }
            jQuery.ajax(url, {
                dataType: 'json',// 服务器返回json格式数据
                type: 'post',// HTTP请求类型
                data: _data,
                beforeSend: function (XMLHttpRequest) {
                    jQuery.myloading();
                },
                complete: function (XMLHttpRequest, status) {
                    jQuery.myloading("hide");
                },
                success: function (data) {
                    var res = data.res;
                    if (res == "0") {
                        jQuery('#deptOfUserWithMultiDept').val(deptCode);
                        if ("users" in data.result) {
                            var users = data.result.users;
                            if (users.length > 0) {
                                jQuery(".multi_dept_div").remove();
                                jQuery(".dept_title").remove();
                                var _users = self.flowNextUsers('false', users);
                                jQuery(".mui-button-row").before(_users);
                            }
                        }
                    }
                },
                error: function (xhr, type, errorThrown) {
                    // 异常处理；
                    console.log(type);
                }
            });
        },
        flowNextUsers: function (flagXorRadiate, users) {
            console.log('flowNextUsers users', users);
            // users
            var userContent = "";
            if ('' + flagXorRadiate == 'false') {
                var isUserSelect = false; // 是否为自选用户
                var aryAction = [];
                userContent = '<div class="user-box">';
                userContent += '<div class="mui-input-row user-area"><label>下一步用户:</label></div>';
                $.each(users, function (index, item) {
                    if (!item.value) {
                        return;
                    }
                    var actionTitle = item.actionTitle;
                    var actionUserName = item.actionUserName;
                    var actionUserRealName = item.actionUserRealName;
                    // 自选用户时，服务器端返回不带actionUserName及actionUserRealName
                    if (actionUserName == null) {
                        actionUserName = "";
                        actionUserRealName = "";
                    }
                    var value = item.value;
                    var name = item.name;
                    var realName = item.realName;
                    var roleName = item.roleName;
                    var isSelectable = item.isSelectable === 'true';
                    var isSelected = item.isSelected === 'true';
                    var isGoDown = item.isGoDown === 'true';
                    var canSelUser = item.canSelUser === 'true'; // 设置了“选择用户”标志位
                    // console.info( item.canSelUser);
                    var disabled = isSelectable ? "" : "disabled";
                    // 如果根据策略需选中
                    var checked = isSelectable ? "" : "checked";
                    if (isSelected) {
                        checked = "checked";
                    }
                    // 如果只有一个用户，则置为选中状态，20240521 注释，只根据策略来判断是否默认选中
                    /*if (users.length == 1) {
                        checked = "checked";
                    }*/
                    var internalname = item.internalname;
                    // 是否为分支中的下一个 action
                    var isNextAction = false;
                    if (users.length > 1) {
                        if (aryAction.length > 0 && aryAction[aryAction.length - 1] != internalname) {
                            isNextAction = true;
                        } else if (aryAction.length == 0) {
                            isNextAction = true;
                        }

                    } else {
                        isNextAction = true;
                    }
                    console.log('aryAction', aryAction, 'isNextAction', isNextAction, 'internalname', internalname);

                    var isFirstUserInAction = false;
                    if (!aryAction.some((it) => it === internalname)) {
                        aryAction.push(internalname);
                        isFirstUserInAction = true;
                    }
                    if (isFirstUserInAction) {
                        userContent += '<div id="' + item.name + '_box"><div class="mui-input-row"><label>' + actionTitle + ':</label></div>';
                    }
                    if (canSelUser || actionUserName == "$userSelect" || value == "$userSelect") {
                        isUserSelect = true;
                        // XorNextActionInternalNames已改为在提交时通过被选中人员的data-internalname获取
                        // userContent += "<input type='hidden' name='XorNextActionInternalNames' value='"+internalname+"' />";

                        // 使每个action分支，只出现1个选择用户按钮
                        console.log('users.length - 1', users.length - 1, 'index', index);
                        if (isNextAction/* || users.length - 1 == index*/) {
                            if (isUserSelect) {
                                userContent += '<div class="mui-row user-area">';
                                userContent += '<button type="button" name="' + name + '" class="mui-btn mui-btn-primary choose_user_btn" isGoDown="' + isGoDown + '" internalName="' + internalname + '" style="margin: 10px;float:right;" >选择用户</button>';
                                userContent += '</div>';
                            }
                        }
                        
                        // checked = "checked";
                        userContent += '<div id="next_user' + internalname + '" class="mui-row next_user_div user-area">';
                        userContent += '<div class="user_ck_span user-area" style="margin-left:15px">';
                        if (!(actionUserName == "$userSelect" || value == "$userSelect")) {
                            userContent += '<span class="mui-checkbox mui-left user_ck_span" style="float:left;">';
                            userContent += '<label style="line-height: 45px;">';
                            userContent += realName;
                            userContent += '</lable>';
                            // 如果不可选择，则需用hidden，而不能用disabled，因为后者不会被提交，而后端FinishAction中 ”以前台选择的为准“
                            if (disabled == 'disabled') {
                                userContent += '<input data-internalname="' + internalname + '" name="' + name + '" value="' + value + '" type="hidden" class="next_user_ck" />'
                            } else {
                                userContent += '<input data-internalname="' + internalname + '" name="' + name + '" value="' + value + '" type="checkbox" ' + checked + ' class="next_user_ck" />'
                            }
                            userContent += '</span>';
                        }
                        userContent += '</div>';
                        userContent += '</div>';
                    } else {
                        userContent += '<div id="next_user' + internalname + '" class="mui-row next_user_div user-area">';
                        userContent += '<span class="mui-checkbox mui-left user_ck_span" style="float:left;">';
                        userContent += '<label style="line-height: 45px;">';
                        userContent += realName;
                        userContent += '</lable>';
                        // 如果不可选择，则需用hidden，而不能用disabled，因为后者不会被提交，而后端FinishAction中 ”以前台选择的为准“
                        if (disabled == 'disabled') {
                            userContent += '<input data-internalname="' + internalname + '" name="' + name + '" value="' + value + '" type="hidden" class="next_user_ck" />'
                        } else {
                            userContent += '<input data-internalname="' + internalname + '" name="' + name + '" value="' + value + '" type="checkbox" ' + checked + ' class="next_user_ck" />'
                        }
                        userContent += '</span>';
                        userContent += '</div>';
                    }
                    if (isNextAction) {
                        userContent += '</div>';
                    }
                });
                userContent += '</div>';
            }
            return userContent;
        },
        flowNextUsersFree: function () {
            var at_input_box = '';
            at_input_box += '<div class="at_user_div clearfix" isFree="true">';
            at_input_box += '<a><span class="iconfont"></span><span>选择用户</span>';
            at_input_box += '<div class="userDiv"></div>';
            at_input_box += '</a>';
            at_input_box += '</div>';
            return at_input_box;
        },
        conditionBranch: function (users) {
            var self = this;
            var isUserSelect = false;
            var strHtml = '<ul class="mui-table-view condition-view">';
            $.each(users, function (index, item) {
                if (item.value !== '$userSelect') {
                    strHtml += '<li class="mui-table-view-cell mui-checkbox mui-left">';
                    if (!item.isSelectable) {
                        strHtml += '<input data-internalname = "' + item.internalname + '" style="display:none" checked  data-name = "' + item.name + '" type="checkbox" value="' + item.value + '" class="return_ck"/>';
                    } else {
                        var isOnlyOneUser = users.length == 1;
                        if (isOnlyOneUser) {
                            strHtml += '<input type="checkbox" checked disabled style="top:0" />';
                            strHtml += '<input data-internalname = "' + item.internalname + '" data-name= "' + item.name + '" checked style="display:none" type="checkbox" value="' + item.value + '" class="return_ck"/>';
                        } else {
                            strHtml += '<input data-internalname = "' + item.internalname + '" data-name= "' + item.name + '" type="checkbox" style="top:0" value="' + item.value + '" class="return_ck"/>';
                        }
                    }
                    strHtml += item.actionTitle + "：" + item.realName;
                    strHtml += '</li>';
                }
                else {
                    // 当条件中有自选用户
                    isUserSelect = true;
                    strHtml += '<li class="mui-table-view-cell mui-checkbox mui-left"><span class="mui-input-group">';
                    strHtml += '<button type="button" data-name = "' + item.name + '" class="mui-btn mui-btn-primary choose_user_btn" internalName="' + item.internalname + '" style="margin: 10px;float:right;" >选择用户</button>';
                    strHtml += '</span></li>';
                }
            });
            strHtml += '</ul>';
            // 如果是自选用户，则不显示确定按钮，以免误操作
            if (!isUserSelect) {
                strHtml += '<div class="mui-button-row" style="margin-top:10px;">';
                strHtml += '<button class="mui-btn mui-btn-primary" id="return-confirm-btn" type="button" onclick="return false;">确认</button>';
                strHtml += '</div>';
            }
            var pop = new Popup({contentType: 2, isReloadOnClose: false, width: 340, height: 300});
            pop.setContent("contentHtml", strHtml);
            pop.setContent("title", "下一步处理人");
            pop.build();
            pop.show();

            var skey = jQuery("#skey").val();

            // 当条件中有自选用户(或设了选择用户标志位）
            $(".condition-view").on("tap", ".choose_user_btn", function () {
                var checkedValues = [];

                // 加入在分支线所选的用户
                var _ckChecked = jQuery(".return_ck:checked");
                _ckChecked.each(function (i) {
                    var _curCk = jQuery(this);
                    var _val = _curCk.val();
                    checkedValues.push(_val);
                })
                /*jQuery(".next_user_ck").each(function(i) {
                    checkedValues.push(jQuery.trim(jQuery(this).val()));
                });*/

                // szzz的iphone上选择用户界面会变为一个页面，而非一个iframe弹窗
                pop.close();

                var internalName = jQuery(this).attr('internalName');
                var chooseUser = checkedValues.join(",");
                var isCondition = true;
                var workflowActionIdStr = jQuery(this).data('name');
                openChooseUser(chooseUser, false, false, internalName, isCondition, workflowActionIdStr, skey);
            });

            // 使”下一步处理人"弹出对话框的背景色为白色
            jQuery('#dialogBox').css('background-color', '#fff');
            mui(".mui-button-row").on("tap", "#return-confirm-btn", function () {
                var _ckChecked = jQuery(".return_ck:checked");
                var _checkLen = _ckChecked.length;
                pop.close();
                if (_checkLen > 0) {
                    jQuery("#flow_form").append('<input type="hidden" name="isAfterSaveformvalueBeforeXorCondSelect" value="true" />');
                    _ckChecked.each(function (i) {
                        var _curCk = jQuery(this);
                        var _internalname = _curCk.data("internalname");
                        var _actionName = _curCk.data("name");
                        var _val = _curCk.val();
                        jQuery("#flow_form").append('<input type="hidden" name="XorNextActionInternalNames" value="' + _internalname + '" />');
                        jQuery("#flow_form").append('<input type="hidden" name="' + _actionName + '" value="' + _val + '" />');
                    })
                    self.flowSendServer();
                }
            });
        },
        onSelUserForCondition: function (userNames, internalName, actionName) {
            // 当条件中有自选用户时调用
            var self = this;
            jQuery("#flow_form").append('<input type="hidden" name="isAfterSaveformvalueBeforeXorCondSelect" value="true" />');
            jQuery("#flow_form").append('<input type="hidden" name="XorNextActionInternalNames" value="' + internalName + '" />');
            jQuery("#flow_form").append('<input type="hidden" name="' + actionName + '" value="' + userNames + '" />');
            self.flowSendServer();
        },
        // 退回
        backUsers: function (users) {
            var self = this;
            var strHtml = '<ul class="mui-table-view">';
            $.each(users, function (index, item) {
                strHtml += '<li class="mui-table-view-cell mui-radio mui-left">';
                strHtml += '<input type="radio" style="top:0" value="' + item.id + '" name="returnUsers" class="return_ck"/>';
                strHtml += item.actionTitle + ":" + item.name;
                strHtml += '</li>';
            });
            strHtml += '</ul>';

            var isFlowReturnWithRemark = jQuery(".back_submit").attr("isFlowReturnWithRemark") == "true";
            if (isFlowReturnWithRemark) {
                strHtml += '<div class="mui-input-row">';
                strHtml += '<label>留言</label>';
                strHtml += '</div>';
                strHtml += '<div style="text-align: center"><textarea id="returnRemark" style="border: 1px solid #eee; width:90%"></textarea></div>';
            }
            strHtml += '<div class="mui-button-row" style="margin-top:10px;">';
            strHtml += '<button class="mui-btn mui-btn-primary" id="return-back-confirm-btn" type="button" onclick="return false;">确认</button>';
            strHtml += '</div>';

            var pop = new Popup({contentType: 2, isReloadOnClose: false, width: 340, height: 300});
            pop.setContent("contentHtml", strHtml);
            pop.setContent("title", "请选择要返回的人员");
            pop.build();
            pop.show();
            // 使弹出对话框的背景色为白色
            jQuery('#dialogBox').css('background-color', '#fff');
            mui(".mui-button-row").on("tap", "#return-back-confirm-btn", function () {
                var _ckChecked = jQuery(".return_ck:checked");
                var _checkLen = _ckChecked.length;
                if (_checkLen == 0) {
                    mui.toast('请选择人员');
                    return;
                }
                if (isFlowReturnWithRemark) {
                    if (jQuery('#returnRemark').val() == '') {
                        mui.toast('请填写留言');
                        return;
                    }
                }
                jQuery('#cwsWorkflowResult').val(jQuery('#returnRemark').val());
                pop.close();

                if (_checkLen > 0) {
                    // 如果进度的回复未填，会导致需填写后重新再次取返回用户，有可能会导致returnId重复，所以此处需清除
                    jQuery("input[name='returnId']").remove();

                    _ckChecked.each(function (i) {
                        var _curCk = jQuery(this);
                        var _val = _curCk.val();
                        jQuery("#flow_form").append('<input type="hidden" name="returnId" value="' + _val + '" />');
                    })
                    jQuery('#op').val('return');
                    self.flowSendServer();
                }
            });
        },
        bindDataEvent: function () {
            var self = this;
            var content = self.element;
            var skey = jQuery("#skey").val();
            // 选择用户
            $(".mui-input-group").on("tap", ".choose_user_btn", function () {
                var checkedValues = [];
                jQuery(".next_user_ck").each(function (i) {
                    checkedValues.push(jQuery.trim(jQuery(this).val()));
                });
                var internalName = jQuery(this).attr('internalName');
                var chooseUser = checkedValues.join(",");

                console.log('chooseUser', chooseUser);
                var departmentLimited = deptLimited;
                if (deptFieldLimited) {
                    let df = o(deptFieldLimited).value;
                    if (departmentLimited == '') departmentLimited = df;
                    else departmentLimited += ',' + df;

                    // 转为数组并去重
                    let deptAry = departmentLimited.split(',');
                    deptAry = deptAry.filter(function (item, index) {
                        return deptAry.indexOf(item) === index;
                    });
                    departmentLimited = deptAry.join(',');
                }
                openChooseUser(chooseUser, false, false, internalName, false, '', skey, departmentLimited);
            });

            Form.bindFileDel();

            // iphone只能用原生的方式来绑定事件
            if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                var btnCapture = $('.capture_btn')[0];
                if (btnCapture) {
                    btnCapture.onclick = function () {
                        captureFieldName = jQuery(btnCapture).attr("captureFieldName");
                        // 置图像宏控件是否只允许拍照
                        if (jQuery(btnCapture).attr("isOnlyCamera")) {
                            setIsOnlyCamera(jQuery(btnCapture).attr("isOnlyCamera"));
                        } else {
                            // 恢复默认设置
                            resetIsOnlyCamera();
                        }
                        // 如果只允许拍照
                        if (appProp.isOnlyCamera == "true") {
                            jQuery("#captureFile").attr('capture', 'camera');
                        }
                        var cap = jQuery("#captureFile").get(0);
                        cap.click();
                        // 会出错，因为页面中可能含有多个captureFile
                        // document.getElementById('captureFile').click();
                    }
                }
            } else {
                mui(".mui-button-row, .mui-input-row").on("tap", ".capture_btn", function () {
                    captureFieldName = jQuery(this).attr("captureFieldName");
                    // 置图像宏控件是否只允许拍照
                    if (jQuery(this).attr("isOnlyCamera")) {
                        setIsOnlyCamera(jQuery(this).attr("isOnlyCamera"));
                    } else {
                        // 恢复默认设置
                        resetIsOnlyCamera();
                    }

                    if (appProp.isOnlyCamera == "true") {
                        jQuery("#captureFile").attr('capture', 'camera');
                    }
                    var cap = jQuery("#captureFile").get(0);
                    cap.click();
                });
            }

            // 自由流程、@流程选择用户
            $("form").on("tap", ".at_user_div", function () {
                var checkedValues = [];
                jQuery(".free_next_user_ck").each(function (i) {
                    checkedValues.push(jQuery(this).val());
                })
                var chooseUser = checkedValues.join(",");
                var isLight = jQuery(".at_user_div").attr("isLight") == "true";
                var isFree = jQuery(".at_user_div").attr("isFree") == "true";
                openChooseUser(chooseUser, isLight, isFree);
            });

            $("form").on("tap", ".flow-mine-title", function () {
                var flowId = jQuery(this).attr('flowId');
                mui.openWindow('../flow/flowShow?skey=' + skey + '&flowId=' + flowId + '&isUniWebview=' + self.options.isUniWebview);
            });
        }
    })
})(mui, window, document)
